package com.haixiaoke.saas.user.service.impl;

import java.util.List;

import cn.hutool.core.util.IdcardUtil;
import com.haixiaoke.saas.common.constant.UserConstants;
import com.haixiaoke.saas.common.utils.DateUtils;
import com.haixiaoke.saas.order.domain.OrderLessee;
import com.haixiaoke.saas.user.domain.CheckInPerson;
import com.haixiaoke.saas.user.mapper.CheckInPersonMapper;
import com.haixiaoke.saas.user.service.ICheckInPersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 入住人管理Service业务层处理
 *
 * @author panda
 * @date 2024-04-11
 */
@Service
public class CheckInPersonServiceImpl implements ICheckInPersonService {
    @Autowired
    private CheckInPersonMapper checkInPersonMapper;

    /**
     * 查询入住人管理
     *
     * @param personId 入住人管理主键
     * @return 入住人管理
     */
    @Override
    public CheckInPerson selectCheckInPersonByPersonId(Long personId) {
        return checkInPersonMapper.selectCheckInPersonByPersonId(personId);
    }

    /**
     * 查询入住人管理列表
     *
     * @param checkInPerson 入住人管理
     * @return 入住人管理
     */
    @Override
    public List<CheckInPerson> selectCheckInPersonList(CheckInPerson checkInPerson) {
        return checkInPersonMapper.selectCheckInPersonList(checkInPerson);
    }

    /**
     * 新增入住人管理
     *
     * @param checkInPerson 入住人管理
     * @return 结果
     */
    @Override
    public int insertCheckInPerson(CheckInPerson checkInPerson) {
        if (UserConstants.DEFAULT_STATE_YES.equals(checkInPerson.getDefaultState())) {
            this.updateDefaultStateByLastPerson(checkInPerson.getUserId(), null);
        }
        int genderByIdCard = IdcardUtil.getGenderByIdCard(checkInPerson.getIdCard());
        // genderByIdCard  1男  2女
        checkInPerson.setSex(genderByIdCard == 1 ? OrderLessee.LESSEE_SEX_MAN : OrderLessee.LESSEE_SEX_WOMAN);
        checkInPerson.setCreateTime(DateUtils.getNowDate());
        return checkInPersonMapper.insertCheckInPerson(checkInPerson);
    }

    /**
     * 修改入住人管理
     *
     * @param checkInPerson 入住人管理
     * @return 结果
     */
    @Override
    public int updateCheckInPerson(CheckInPerson checkInPerson) {
        if (UserConstants.DEFAULT_STATE_YES.equals(checkInPerson.getDefaultState())) {
            this.updateDefaultStateByLastPerson(checkInPerson.getUserId(), checkInPerson.getPersonId());
        }
        checkInPerson.setUpdateTime(DateUtils.getNowDate());
        checkInPerson.setPersonName(null);
        checkInPerson.setIdCard(null);
        return checkInPersonMapper.updateCheckInPerson(checkInPerson);
    }

    /**
     * 批量删除入住人管理
     *
     * @param personIds 需要删除的入住人管理主键
     * @return 结果
     */
    @Override
    public int deleteCheckInPersonByPersonIds(Long[] personIds) {
        return checkInPersonMapper.deleteCheckInPersonByPersonIds(personIds);
    }

    /**
     * 删除入住人管理信息
     *
     * @param personId 入住人管理主键
     * @return 结果
     */
    @Override
    public int deleteCheckInPersonByPersonId(Long personId) {
        return checkInPersonMapper.deleteCheckInPersonByPersonId(personId);
    }

    @Override
    public int updateDefaultStateByLastPerson(Long userId, Long personId) {
        return checkInPersonMapper.updateDefaultStateByLastPerson(userId, personId);
    }

    @Override
    public int softDeleteCheckInPersonByPersonIds(Long[] personIds) {
        return checkInPersonMapper.softDeleteCheckInPersonByPersonIds(personIds);
    }

    @Override
    public boolean checkPersonByPhone(Long personId, Long userId, String personPhone) {
        int result = checkInPersonMapper.checkPersonByPhone(personId, userId, personPhone);
        return result > 0;
    }

    @Override
    public CheckInPerson selectDefaultPersonByUserId(Long userId) {
        return checkInPersonMapper.selectDefaultPersonByUserId(userId);
    }

    @Override
    public List<CheckInPerson> selectCheckInPersonByPersonIds(List<Long> personIds) {
        return checkInPersonMapper.selectCheckInPersonByPersonIds(personIds);
    }

    @Override
    public CheckInPerson selectPersonByIdCard(String idCard, Long userId) {
        return checkInPersonMapper.selectPersonByIdCard(idCard,userId);
    }
}
